package com.gator.desig_mode.obsever;



import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public interface Subject {

  //注册
  void observerRegister(Observer observer);


  //移除
  void observerRemove(Observer observer);


  //通知
  void observerNotify();

}


class WeatherSubject implements Subject {



  private static class SingletonWeatherSubject{
    private static final WeatherSubject weatherSubject = new WeatherSubject();
  }


  private WeatherSubject() {
  }

  public static WeatherSubject getWeaObj(){
    list = new ArrayList<>();
    return SingletonWeatherSubject.weatherSubject;
  }


  //数据结构
  private static List<Observer> list;

  private Info info;

  public List<Observer> getList() {
    return list;
  }

  public void setList(List<Observer> list) {
    this.list = list;
  }

  public Info getInfo() {
    return info;
  }

  public void setInfo(Info info) {
    this.info = info;
  }


  @Override
  public void observerRegister(Observer observer) {

    list.add(observer);

  }

  @Override
  public void observerRemove(Observer observer) {
    list.remove(observer);

  }


  //通知观察者
  //和观察者循环查询信息相比，被观察者推送数据会更好
  @Override
  public void observerNotify() {

    for (Observer item : list
    ) {
      item.update(info);
    }

  }
}
